package com.cssy.yd.logic.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.base.web.BodyMsg;
import com.cssy.yd.logic.dao.LogicOtherInwhDAO;
import com.cssy.yd.logic.dao.LogicOtherInwhItemDAO;
import com.cssy.yd.logic.dao.LogicPoDAO;
import com.cssy.yd.logic.dao.LogicPoItemDAO;
import com.cssy.yd.logic.dao.LogicPoWareDAO;
import com.cssy.yd.logic.dao.LogicPoWareItemBatchDAO;
import com.cssy.yd.logic.dao.LogicPoWareItemDAO;
import com.cssy.yd.logic.dao.LogicPriceDAO;
import com.cssy.yd.logic.entity.LogicOtherInwhDO;
import com.cssy.yd.logic.entity.LogicOtherInwhItemDO;
import com.cssy.yd.logic.entity.LogicPoDO;
import com.cssy.yd.logic.entity.LogicPoItemDO;
import com.cssy.yd.logic.entity.LogicPoWareDO;
import com.cssy.yd.logic.entity.LogicPoWareItemBatchDO;
import com.cssy.yd.logic.entity.LogicPoWareItemDO;
import com.cssy.yd.logic.entity.LogicPriceDO;
import com.cssy.yd.logic.entity.LogicWhAffDO;
import com.cssy.yd.logic.entity.ReportCqtjDO;
import com.cssy.yd.logic.entity.ReportCqtjItemDO;
import com.cssy.yd.logic.service.ILogicPoWareService;
import com.cssy.yd.logic.service.ILogicWhAffStockService;
import com.cssy.yd.logic.util.BillCode;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.exception.SyCodeException;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicPoWareService")
public class LogicPoWareServiceImpl implements ILogicPoWareService{
	@Resource(name = "logicPoWareDAO")
	private LogicPoWareDAO logicPoWareDAO;
	
	@Resource(name = "logicPoDAO")
	private LogicPoDAO logicPoDAO;
	
	@Resource(name = "logicPoItemDAO")
	private LogicPoItemDAO logicPoItemDAO;
	
	@Resource(name = "logicPoWareItemDAO")
	private LogicPoWareItemDAO logicPoWareItemDAO;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name = "logicWhAffStockService")
	private ILogicWhAffStockService logicWhAffStockService;
	
	@Resource(name = "logicOtherInwhDAO")
	private LogicOtherInwhDAO logicOtherInwhDAO;
	@Resource(name = "logicOtherInwhItemDAO")
	private LogicOtherInwhItemDAO logicOtherInwhItemDAO;
	
	@Resource(name = "logicPriceDAO")
	private LogicPriceDAO logicPriceDAO;
	
	@Resource(name="logicPoWareItemBatchDAO")
	private LogicPoWareItemBatchDAO logicPoWareItemBatchDAO;
	boolean flag = false;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicPoWareDO>  list=logicPoWareDAO.find(scs);
		rs.setRecordCount(logicPoWareDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		Integer i =logicPoWareDAO.deleteLogicPoWareDOByPrimaryKey(id);
		return null;
	}

	@Override
	public Integer update(LogicPoWareDO t, User user) throws SyBaseException {
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		return logicPoWareDAO.updateLogicPoWareDO(t);
	}

	@Override
	public LogicPoWareDO save(LogicPoWareDO t, User user) throws SyBaseException {
		t.setDcruser(user.getName());
		t.setDctime(new Date());
		t.setId(IDCodeTypeUtil.getId());
		logicPoWareDAO.insertLogicPoWareDO(t);
		return t;
	}

	@Override
	public LogicPoWareDO findListById(String id) throws SyBaseException {
		return logicPoWareDAO.findLogicPoWareDOByPrimaryKey(id);
	}
	
	@Override
	public SearchResult findItemListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicPoWareItemDO>  list=logicPoWareItemDAO.find(scs);
		rs.setRecordCount(logicPoWareItemDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer deleteItem(String id) throws SyBaseException {
		Integer i =logicPoWareItemDAO.deleteLogicPoWareItemDOByPrimaryKey(id);
		return null;
	}

	@Override
	public Integer updateItem(LogicPoWareItemDO t) throws SyBaseException {
		return logicPoWareItemDAO.updateLogicPoWareItemDO(t);
	}

	@Override
	public LogicPoWareItemDO saveItem(LogicPoWareItemDO t) throws SyBaseException {
		t.setId(IDCodeTypeUtil.getId());
		logicPoWareItemDAO.insertLogicPoWareItemDO(t);
		return t;
	}

	@Override
	public LogicPoWareItemDO findItemListById(String id) throws SyBaseException {
		return logicPoWareItemDAO.findLogicPoWareItemDOByPrimaryKey(id);
	}

	@Override
	public List<LogicPoWareItemDO> findItemListBypoId(String id)
			throws SyBaseException {
		return logicPoWareItemDAO.finditembypoid(id);
	}

	@Override
	public void waresave(HttpServletRequest request,
			LogicPoWareDO curAP) throws SyBaseException, SyCodeException {
		try {
		String itemdata =request.getParameter("itemdata");
		String itemdelete =request.getParameter("itemdelete");
		String rkdate=request.getParameter("rkdate");//入库日期
		String type=request.getParameter("type");
		String bhid=request.getParameter("id");
		if("bh".equals(type)){
			LogicPoWareDO poware = logicPoWareDAO.findLogicPoWareDOByPrimaryKey(bhid);
			poware.setBillstatus((short)0);
			this.update(poware,  SessionUtil.getUser(request));
		}else{
//			if("wjdd".equals(type)){
//				type = "3";
//				curAP.setBillstatus(Short.parseShort(type));
//				flag = true;
//			}else{
//				curAP.setBillstatus(Short.parseShort(type));
//			}
			curAP.setBillstatus(Short.parseShort(type));
			curAP.setRkdate(DateUtils.stringToDate(rkdate));
			List<LogicPoWareItemDO> axxlist=JSONArray.parseArray(itemdata, LogicPoWareItemDO.class);
				if (curAP.getId() != null && !"".equals(curAP.getId())) {// 修改
					curAP.setDuuser(SessionUtil.getUser(request).getName());
					curAP.setDutime(new Date());
					int rst=logicPoWareDAO.updateLogicPoWareDO(curAP);
					if(rst==1){
						if(itemdelete!=null&&!"".equals(itemdelete)){
							List<LogicPoWareItemDO> lp=JSONArray.parseArray(itemdelete, LogicPoWareItemDO.class);
							for(int i=0;i<lp.size();i++){
//								SearchConditions scs = new SearchConditions();
//								scs.setRemovedProperty(null);
//								scs.addCondition("id", lp.get(i).getId(),SearchCondition.OP_EQUAL);
//								scs.addCondition("powareid", curAP.getId(),SearchCondition.OP_EQUAL);
//								scs.addCondition("materialsid", lp.get(i).getMaterialsid(),SearchCondition.OP_EQUAL);
//								scs.addCondition("code", lp.get(i).getCode(),SearchCondition.OP_EQUAL);
//								scs.addCondition("batch", lp.get(i).getBatch(),SearchCondition.OP_EQUAL);
//								scs.addCondition("state", lp.get(i).getState(),SearchCondition.OP_EQUAL);
//								List<LogicPoWareItemDO> itemDOs = logicPoWareItemDAO.find(scs);
								if((lp.get(i).getId()!=null && !"".equals(lp.get(i).getId()))){
									logicPoWareItemDAO.deleteLogicPoWareItemDOByPrimaryKey(lp.get(i).getId());
								}
							}
						}
						for(int i=0;i<axxlist.size();i++){
							String axid = "";
							axxlist.get(i).setPowareid(curAP.getId());
							if((axxlist.get(i).getId()!=null && !"".equals(axxlist.get(i).getId()))){
								axid = axxlist.get(i).getId();
								logicPoWareItemDAO.updateLogicPoWareItemDO(axxlist.get(i));
							}else{
								axid = IDCodeTypeUtil.getId();
								axxlist.get(i).setId(axid);
								logicPoWareItemDAO.insertLogicPoWareItemDO(axxlist.get(i));
							}
//							if(axxlist.get(i).getDelItemBatchDO() != null && !"".equals(axxlist.get(i).getDelItemBatchDO())){
//								List<LogicPoWareItemBatchDO> delItemBatchDOs=JSONArray.parseArray(axxlist.get(i).getDelItemBatchDO(), LogicPoWareItemBatchDO.class);
//								for (int j = 0; j < delItemBatchDOs.size(); j++) {
//									SearchConditions scs = new SearchConditions();
//									scs.setRemovedProperty(null);
//									scs.addCondition("id", delItemBatchDOs.get(j).getId(),SearchCondition.OP_EQUAL);
//									scs.addCondition("wareItemId", delItemBatchDOs.get(j).getWareitemid(),SearchCondition.OP_EQUAL);
//									scs.addCondition("materialsid", delItemBatchDOs.get(j).getMaterialsid(),SearchCondition.OP_EQUAL);
//									scs.addCondition("code", delItemBatchDOs.get(j).getCode(),SearchCondition.OP_EQUAL);
//									scs.addCondition("batch", delItemBatchDOs.get(j).getBatch(),SearchCondition.OP_EQUAL);
//									scs.addCondition("state", delItemBatchDOs.get(j).getState(),SearchCondition.OP_EQUAL);
//									List<LogicPoWareItemBatchDO> itemDOs = logicPoWareItemBatchDAO.find(scs);
//									if(itemDOs != null && itemDOs.size() > 0){
//										logicPoWareItemBatchDAO.deleteLogicPoWareItemBatchDOByPrimaryKey(delItemBatchDOs.get(j).getId());
//									}
//								}
//							}
							logicPoWareItemBatchDAO.deleteLogicPoWareItemBatchDOByWareItemId(axid);
							if(axxlist.get(i).getItemBatchDO() != null && !"".equals(axxlist.get(i).getItemBatchDO())){
								List<LogicPoWareItemBatchDO> itemBatchDOs=JSONArray.parseArray(axxlist.get(i).getItemBatchDO(), LogicPoWareItemBatchDO.class);
								for (int j = 0; j < itemBatchDOs.size(); j++) {
//									SearchConditions scs = new SearchConditions();
//									scs.setRemovedProperty(null);
//									scs.addCondition("id", itemBatchDOs.get(j).getId(),SearchCondition.OP_EQUAL);
//									scs.addCondition("wareItemId", itemBatchDOs.get(j).getWareitemid(),SearchCondition.OP_EQUAL);
//									scs.addCondition("materialsid", itemBatchDOs.get(j).getMaterialsid(),SearchCondition.OP_EQUAL);
//									scs.addCondition("code", itemBatchDOs.get(j).getCode(),SearchCondition.OP_EQUAL);
//									scs.addCondition("batch", itemBatchDOs.get(j).getBatch(),SearchCondition.OP_EQUAL);
//									scs.addCondition("state", itemBatchDOs.get(j).getState(),SearchCondition.OP_EQUAL);
//									List<LogicPoWareItemBatchDO> itemDOs = logicPoWareItemBatchDAO.find(scs);
//									if((itemDOs.size()>0) &&(itemBatchDOs.get(j).getId()!=null && !"".equals(itemBatchDOs.get(j).getId()))){
//										logicPoWareItemBatchDAO.updateLogicPoWareItemBatchDO(itemBatchDOs.get(j));
//									}else{
//										itemBatchDOs.get(j).setWareitemid(axid);
//										itemBatchDOs.get(j).setId(IDCodeTypeUtil.getId());
//										logicPoWareItemBatchDAO.insertLogicPoWareItemBatchDO(itemBatchDOs.get(j));
//									}
//									List<LogicPoWareItemBatchDO> batchDO = logicPoWareItemBatchDAO.find(scs);
//									if(batchDO.get(0) != null){
//									}
									itemBatchDOs.get(j).setWareitemid(axid);
									itemBatchDOs.get(j).setId(IDCodeTypeUtil.getId());
									logicPoWareItemBatchDAO.insertLogicPoWareItemBatchDO(itemBatchDOs.get(j));
								}
							}
						}
						//录入库存信息
						if("3".equals(type)){
									
							LogicPoWareDO loi = logicPoWareDAO.findLogicPoWareDOByPrimaryKey(curAP.getId());
							//for (LogicPoWareItemDO lii : axxlist){
								for (int i = 0; i < axxlist.size(); i++) {
								//修改采购订单状态和已入库数量
								LogicPoDO ld=logicPoDAO.findcode(curAP.getPocode());
								LogicPoItemDO litem=new LogicPoItemDO();
								litem.setPoId(ld.getId());
								litem.setCode(axxlist.get(i).getCode());
								litem.setName(axxlist.get(i).getName());
								List<LogicPoItemDO> itemlist=logicPoItemDAO.findListByExample(litem);
								if(itemlist.size()>0){
									itemlist.get(0).setInCount((itemlist.get(0).getInCount()==null?0.0:itemlist.get(0).getInCount())+axxlist.get(i).getJquantity());
									if(itemlist.get(0).getInCount()>itemlist.get(0).getJquantity()){
										throw new SyBaseException("错误:当前订单已入库数量总和已经大于订单数量");
									}else{
									logicPoItemDAO.updateLogicPoItemDO(itemlist.get(0));
									axxlist.get(i).setInCount(Double.valueOf(axxlist.get(i).getInCount()==null?0.0:axxlist.get(i).getInCount())+Double.valueOf(axxlist.get(i).getJquantity()==null?0.0:axxlist.get(i).getJquantity()));
									logicPoWareItemDAO.updateLogicPoWareItemDO(axxlist.get(i));
									}
								}else{
									throw new SyBaseException("查不到该物料!");
								}
								ld.setDutime(new Date());
								ld.setDuuser(SessionUtil.getUser(request).getName());
								LogicPoItemDO litem1=new LogicPoItemDO();
								litem1.setPoId(ld.getId());
								List<LogicPoItemDO> itemlist1=logicPoItemDAO.findListByExample(litem1);
								boolean stsuts=true;
								if(itemlist1.size()>0){
									for(int n=0;n<itemlist1.size();n++){
										if((itemlist1.get(n).getJquantity()==null?0.0:itemlist1.get(n).getJquantity())>(itemlist1.get(n).getInCount()==null?0.0:itemlist1.get(n).getInCount())){
											stsuts=false;
										}
									}
								}
//								if(flag){
//										ld.setBillstatus("5");
//										logicPoDAO.updateLogicPoDO(ld);
//										if(i==(axxlist.size()-1)){
//											flag =false;
//										}
//								}else{
									if(stsuts){
										ld.setBillstatus("5");
										logicPoDAO.updateLogicPoDO(ld);
									}else{
										ld.setBillstatus("4");
										logicPoDAO.updateLogicPoDO(ld);
									}
//								}
								
								if (curAP.getBillstatus() == 3){
//									if(axxlist.get(i).getId() != null && !"".equals(axxlist.get(i).getId())){
//										LogicPoWareItemBatchDO batchDO = new LogicPoWareItemBatchDO();
//										batchDO.setWareitemid(axxlist.get(i).getId());
//										batchDO.setCode(axxlist.get(i).getCode());
//										List<LogicPoWareItemBatchDO> itemBatchDOs=logicPoWareItemBatchDAO.findListByExample(batchDO);
									if(axxlist.get(i).getItemBatchDO() != null && "Y".equals(axxlist.get(i).getOpenBatch())){
										if(axxlist.get(i).getItemBatchDO() != null && !"".equals(axxlist.get(i).getItemBatchDO())){
											List<LogicPoWareItemBatchDO> itemBatchDOs=JSONArray.parseArray(axxlist.get(i).getItemBatchDO(), LogicPoWareItemBatchDO.class);	
											for (int j = 0; j < itemBatchDOs.size(); j++) {
												LogicWhAffDO lw = new LogicWhAffDO();
												lw.setBillCode(loi.getPowarecode());
												lw.setBatch(itemBatchDOs.get(j).getBatch());
												lw.setBillType("PW");
												lw.setCode(axxlist.get(i).getCode());
												lw.setDcruser(SessionUtil.getUser(request).getName());
												lw.setDctime(new Date());
												lw.setName(axxlist.get(i).getName());
												lw.setQuantity(itemBatchDOs.get(j).getQuantity());
												lw.setSpec(axxlist.get(i).getSpec());
												lw.setStatus(itemBatchDOs.get(j).getState());
												lw.setBrand(axxlist.get(i).getBrand());
												lw.setHubName(curAP.getWhname());
												lw.setWhCode(loi.getWhcode());
												lw.setWhname(loi.getWhname());
												lw.setUnit(axxlist.get(i).getUnit());
												lw.setJunit(axxlist.get(i).getJunit());
												lw.setJquantity(itemBatchDOs.get(j).getJquantity());
												lw.setBzq(axxlist.get(i).getBzq());
												lw.setYxq(axxlist.get(i).getYxq());
												lw.setScrq(axxlist.get(i).getScrq());
												if(axxlist.get(i).getPrice()==null||axxlist.get(i).getPrice()<=0){
													SearchConditions scs = new SearchConditions();
													scs.addCondition("wh_code", loi.getWhcode(),SearchCondition.OP_EQUAL);
													scs.addCondition("code", axxlist.get(i).getCode(),SearchCondition.OP_EQUAL);
													List<LogicPriceDO> lp=logicPriceDAO.find(scs);
													if(lp.size()>0){
														if(lp.get(0).getSumprice()!=null&&lp.get(0).getSumprice()>0){
															lw.setPrice(lp.get(0).getSumprice());
														}else{
															throw new SyBaseException("您没有填写单价,并且查不到该天物料的平均价格为0！入库失败！请填写物料价格");
														}
													}else{
														throw new SyBaseException("您没有填写单价,并且查不到该天物料的平均价格！入库失败！请填写物料价格");
													}
												}else{
													lw.setPrice(Double.parseDouble(ld.getExchangeRate())*axxlist.get(i).getPrice());
												}
												logicWhAffStockService.updateWhAffstockQuantity(lw, SessionUtil.getUser(request),true);
											}
										}
									}else{
										LogicWhAffDO lw = new LogicWhAffDO();
										lw.setBillCode(loi.getPowarecode());
										lw.setBatch("NoBatch");
										lw.setBillType("PW");
										lw.setCode(axxlist.get(i).getCode());
										lw.setDcruser(SessionUtil.getUser(request).getName());
										lw.setDctime(new Date());
										lw.setName(axxlist.get(i).getName());
										lw.setQuantity(axxlist.get(i).getQuantity());
										lw.setSpec(axxlist.get(i).getSpec());
										lw.setStatus(axxlist.get(i).getState());
										lw.setBrand(axxlist.get(i).getBrand());
										lw.setHubName(curAP.getWhname());
										lw.setWhCode(loi.getWhcode());
										lw.setWhname(loi.getWhname());
										lw.setUnit(axxlist.get(i).getUnit());
										lw.setJunit(axxlist.get(i).getJunit());
										lw.setJquantity(axxlist.get(i).getJquantity());
										lw.setBzq(axxlist.get(i).getBzq());
										lw.setYxq(axxlist.get(i).getYxq());
										lw.setScrq(axxlist.get(i).getScrq());
										if(axxlist.get(i).getPrice()==null||axxlist.get(i).getPrice()<=0){
											SearchConditions scs = new SearchConditions();
											scs.addCondition("wh_code", loi.getWhcode(),SearchCondition.OP_EQUAL);
											scs.addCondition("code", axxlist.get(i).getCode(),SearchCondition.OP_EQUAL);
											List<LogicPriceDO> lp=logicPriceDAO.find(scs);
											if(lp.size()>0){
												if(lp.get(0).getSumprice()!=null&&lp.get(0).getSumprice()>0){
													lw.setPrice(lp.get(0).getSumprice());
												}else{
													throw new SyBaseException("您没有填写单价,并且查不到该天物料的平均价格为0！入库失败！请填写物料价格");
												}
											}else{
												throw new SyBaseException("您没有填写单价,并且查不到该天物料的平均价格！入库失败！请填写物料价格");
											}
										}else{
											lw.setPrice(Double.parseDouble(ld.getExchangeRate())*axxlist.get(i).getPrice());
										}
										logicWhAffStockService.updateWhAffstockQuantity(lw, SessionUtil.getUser(request),true);
									}
									
								}
							}
						}
					}else{
						throw new SyBaseException("您修改的当前行数据已发生改变,请重新点开查看");
					}
				} else {// 新增
					String id=IDCodeTypeUtil.getId();
					curAP.setFkstatus(0);
					curAP.setPowarecode(syCodeService.getCode("PW","PW",SyCode.DATE_YYMM,5));
					curAP.setBillstatus(Short.parseShort("0"));
					curAP.setId(id);
					curAP.setDcruser(SessionUtil.getUser(request).getName());
					curAP.setDctime(new Date());
//					curAP.setBatch("PW"+curAP.getBatch());
					logicPoWareDAO.insertLogicPoWareDO(curAP);
					for(int i=0;i<axxlist.size();i++){
						axxlist.get(i).setPowareid(id);
						String axid = IDCodeTypeUtil.getId();
						axxlist.get(i).setId(axid);
						logicPoWareItemDAO.insertLogicPoWareItemDO(axxlist.get(i));
						if(axxlist.get(i).getItemBatchDO() != null){
							List<LogicPoWareItemBatchDO> itemBatchDOs=JSONArray.parseArray(axxlist.get(i).getItemBatchDO(), LogicPoWareItemBatchDO.class);
							for (int j = 0; j < itemBatchDOs.size(); j++) {
								itemBatchDOs.get(j).setId(IDCodeTypeUtil.getId());
								itemBatchDOs.get(j).setWareitemid(axid);
								logicPoWareItemBatchDAO.insertLogicPoWareItemBatchDO(itemBatchDOs.get(j));
							}
						}
						
					}
				}
		}	
		} catch (Exception e) {
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}
	
	@Override
	public void deleteByPid(String pid) throws SyBaseException {
//		logicPoWareItemDAO.deleteByPid(pid);
		
	}
	@Override
	public List<ReportCqtjItemDO> findListByReportCqtjDO(ReportCqtjDO scs)
			throws SyBaseException {
		List<ReportCqtjDO> repcqlist = logicPoWareItemDAO.findListByReportCqtjDO(scs);
		List<ReportCqtjDO> returnlist = logicPoWareItemDAO.findbyReturn(scs);
		List<String> namelist = logicPoWareItemDAO.findListByName(scs);
		List<ReportCqtjItemDO> repcqitemlist = new ArrayList<ReportCqtjItemDO>();
		for(String strname : namelist){
			ReportCqtjItemDO reportCqtjItemDO = new ReportCqtjItemDO();
			double total = 0 ;
			reportCqtjItemDO.setName(strname);
			for(ReportCqtjDO repcq : repcqlist){
				if(strname.equals(repcq.getName())){
					if("01".equals(repcq.getRq())){
						reportCqtjItemDO.setJan(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("02".equals(repcq.getRq())){
						reportCqtjItemDO.setFeb(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("03".equals(repcq.getRq())){
						reportCqtjItemDO.setMar(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("04".equals(repcq.getRq())){
						reportCqtjItemDO.setApr(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("05".equals(repcq.getRq())){
						reportCqtjItemDO.setMay(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("06".equals(repcq.getRq())){
						reportCqtjItemDO.setJun(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("07".equals(repcq.getRq())){
						reportCqtjItemDO.setJul(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("08".equals(repcq.getRq())){
						reportCqtjItemDO.setAug(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("09".equals(repcq.getRq())){
						reportCqtjItemDO.setSep(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("10".equals(repcq.getRq())){
						reportCqtjItemDO.setOct(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("11".equals(repcq.getRq())){
						reportCqtjItemDO.setNov(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("12".equals(repcq.getRq())){
						reportCqtjItemDO.setDec(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
				}
			}
			for(ReportCqtjDO repcqreturn : returnlist){
				if(strname.equals(repcqreturn.getName())){
					if("01".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setJan(reportCqtjItemDO.getJan() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("02".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setFeb(reportCqtjItemDO.getFeb() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("03".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setMar(reportCqtjItemDO.getMar() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("04".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setApr(reportCqtjItemDO.getApr() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("05".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setMay(reportCqtjItemDO.getMay() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("06".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setJun(reportCqtjItemDO.getJun() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("07".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setJul(reportCqtjItemDO.getJul() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("08".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setAug(reportCqtjItemDO.getAug() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("09".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setSep(reportCqtjItemDO.getSep() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("10".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setOct(reportCqtjItemDO.getOct() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("11".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setNov(reportCqtjItemDO.getNov() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("12".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setDec(reportCqtjItemDO.getDec() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
				}
			}
			reportCqtjItemDO.setTotal(total);
			repcqitemlist.add(reportCqtjItemDO);
		}
		return repcqitemlist;
	}
	@Override
	public List<ReportCqtjItemDO> findListByWh(ReportCqtjDO scs)
			throws SyBaseException {
		List<ReportCqtjDO> repcqlist = logicPoWareItemDAO.findListByWh(scs);
		List<ReportCqtjDO> returnlist = logicPoWareItemDAO.findbyWhReturn(scs);
		List<String> namelist = logicPoWareItemDAO.findListByWhName(scs);
		List<ReportCqtjItemDO> repcqitemlist = new ArrayList<ReportCqtjItemDO>();
		for(String strname : namelist){
			ReportCqtjItemDO reportCqtjItemDO = new ReportCqtjItemDO();
			double total = 0 ;
			reportCqtjItemDO.setName(strname);
			for(ReportCqtjDO repcq : repcqlist){
				if(strname.equals(repcq.getName())){
					if("01".equals(repcq.getRq())){
						reportCqtjItemDO.setJan(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("02".equals(repcq.getRq())){
						reportCqtjItemDO.setFeb(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("03".equals(repcq.getRq())){
						reportCqtjItemDO.setMar(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("04".equals(repcq.getRq())){
						reportCqtjItemDO.setApr(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("05".equals(repcq.getRq())){
						reportCqtjItemDO.setMay(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("06".equals(repcq.getRq())){
						reportCqtjItemDO.setJun(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("07".equals(repcq.getRq())){
						reportCqtjItemDO.setJul(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("08".equals(repcq.getRq())){
						reportCqtjItemDO.setAug(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("09".equals(repcq.getRq())){
						reportCqtjItemDO.setSep(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("10".equals(repcq.getRq())){
						reportCqtjItemDO.setOct(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("11".equals(repcq.getRq())){
						reportCqtjItemDO.setNov(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
					if("12".equals(repcq.getRq())){
						reportCqtjItemDO.setDec(repcq.getQuantity());
						total += repcq.getQuantity().doubleValue();
					}
				}
			}
			for(ReportCqtjDO repcqreturn : returnlist){
				if(strname.equals(repcqreturn.getName())){
					if("01".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setJan(reportCqtjItemDO.getJan() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("02".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setFeb(reportCqtjItemDO.getFeb() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("03".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setMar(reportCqtjItemDO.getMar() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("04".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setApr(reportCqtjItemDO.getApr() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("05".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setMay(reportCqtjItemDO.getMay() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("06".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setJun(reportCqtjItemDO.getJun() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("07".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setJul(reportCqtjItemDO.getJul() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("08".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setAug(reportCqtjItemDO.getAug() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("09".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setSep(reportCqtjItemDO.getSep() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("10".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setOct(reportCqtjItemDO.getOct() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("11".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setNov(reportCqtjItemDO.getNov() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
					if("12".equals(repcqreturn.getRq())){
						reportCqtjItemDO.setDec(reportCqtjItemDO.getDec() - repcqreturn.getQuantity());
						total -= repcqreturn.getQuantity().doubleValue();
					}
				}
			}
			reportCqtjItemDO.setTotal(total);
			repcqitemlist.add(reportCqtjItemDO);
		}
		return repcqitemlist;
	}

	@Override
	public Integer untreatedPowarecode(Map<String, Object> map)
			throws SyBaseException {
		// TODO Auto-generated method stub
		return logicPoWareDAO.untreatedPowarecode(map);
	}

	@Override
	public SearchResult findAuthorityStatusListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicPoWareDO>  list=logicPoWareDAO.findAuthorityStatus(scs);
		rs.setRecordCount(logicPoWareDAO.findAuthorityStatusCount(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}
}
